package com.lu.servlet;

import com.lu.dao.impl.NewsDaoImpl;
import com.lu.dao.impl.TopicDaoImpl;
import com.lu.entity.News;
import com.lu.entity.Topic;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class NewsEditGetDataServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();

        // 获取新闻修改前的所需要的数据 1 新闻 2 板块主题集合
        int nid = Integer.parseInt(req.getParameter("nid"));
        News news = new NewsDaoImpl().getOneNews(nid);
        List<Topic> listTopic = new TopicDaoImpl().getAllTopic();
        // 放入属性
        req.setAttribute("news",news);
        req.setAttribute("listTopic",listTopic);
        // 转发
        req.getRequestDispatcher("news_edit.jsp").forward(req,resp);
        out.flush();
        out.close();
    }
}
